package com.example.redstarcommerce.cn.dh.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.redstarcommerce.cn.dh.common.vo.Result;
import com.example.redstarcommerce.cn.dh.shop.mapper.ShopMapper;
import com.example.redstarcommerce.cn.dh.shop.pojo.Shop;
import com.example.redstarcommerce.cn.dh.shop.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @ClassName ShopServiceImpl
 * @Author AndyWu
 * @Date 2024-11-01 22:42
 * @Motto 学不死就往死里学
 * @Version 1.0
 */
@Service
@Slf4j
public class ShopServiceImpl implements ShopService{

    @Autowired
    private ShopMapper shopMapper;
    @Override
    public Result<List<Shop>> getAll() {
        List<Shop> shops = shopMapper.selectList(null);
        if (shops.isEmpty()){
            return Result.fail("商品为空");
        }
        Result<List<Shop>> success = Result.success(shops);
        return success;
    }

    @Override
    public Result<Boolean> insertShop(String name, String type, String introduce, Float price) {
        Double scoer = 7.0;
        Integer scoernumber = 0;
        UUID uuid = UUID.randomUUID();
        String uid = uuid.toString();
        Shop shop = new Shop(uid,name,type,introduce,price,scoer,scoernumber);
        int insert = shopMapper.insert(shop);
        if (insert <= 0){
            return Result.fail("插入失败");
        }
        return Result.success(true);
    }

    @Override
    public Result<Boolean> updateShop(String uid, String name, String type, String introduce, Float price, Double scoer, Integer scoernumber) {
        Shop shop = new Shop(uid,name,type,introduce,price,scoer,scoernumber);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        int update = shopMapper.update(shop, queryWrapper);
        if (update <= 0){
            return Result.fail("更新失败");
        }
        return Result.success(true);
    }

    @Override
    public Result<Boolean> deleteShop(String uid) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        int delete = shopMapper.delete(queryWrapper);
        if (delete <= 0){
            return Result.fail("删除失败");
        }
        return Result.success(true);
    }
}
